//
//  CloudDepartmentController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/26.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class CloudDepartmentController: BaseViewController {
    
    private var viewModel: DepartmentViewModel? {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
    }
    
    @IBOutlet private var activityView: UIActivityIndicatorView!

    @IBOutlet private var tableView: UITableView! {
        didSet {
            tableView.rowHeight = 70
            tableView.estimatedRowHeight = UITableView.automaticDimension
            tableView.backgroundColor = .background
            tableView.tableFooterView = UIView()
            tableView.register(CloudDepartmentCell.nibForCell(),
                               forCellReuseIdentifier: CloudDepartmentCell.reuseIdentifier())
        }
    }
    
    private lazy var searchVc: SearchViewController = {
        let resultVc = CloudDepartmentResultController()
        let vc = SearchViewController(searchResultsController: resultVc)
        vc.searchBar.delegate = self
        return vc
    }()
}

extension CloudDepartmentController {
    private func setupUI() {
        title = "找医生"
        definesPresentationContext = true // 搜索push的关键
        tableView.tableHeaderView = searchVc.searchBar
        tableView.mj_header = refreshHeader(action: #selector(pullRefreshData))
    }
    
    private func endRefresh() {
        tableView.mj_header?.endRefreshing()
    }
    
    private func loadData() {
        activityView.startAnimating()
        DepartmentViewModel.getDepartmentList(success: { models in
            self.activityView.stopAnimating()
            self.viewModel = DepartmentViewModel(models: models)
        }) { errorMsg in
            self.activityView.stopAnimating()
            HUDTool.showError(message: errorMsg)
        }
    }
    
    @objc private func pullRefreshData() {
        DepartmentViewModel.getDepartmentList(success: { models in
            self.endRefresh()
            self.viewModel = DepartmentViewModel(models: models)
        }) { errorMsg in
            self.endRefresh()
            HUDTool.showError(message: errorMsg)
        }
    }
}

// MARK: - DataSource & Delegate
extension CloudDepartmentController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel?.numberOfRows ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CloudDepartmentCell.reuseIdentifier(), for: indexPath) as! CloudDepartmentCell
        if let vm = viewModel?.viewModel(for: indexPath) {
            cell.configure(with: vm)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        guard let vm = viewModel?.viewModel(for: indexPath) else { return }
        pushDoctorReserveVc(with: vm)
    }
}

// MARK: - UISearchBarDelegate
extension CloudDepartmentController: UISearchBarDelegate {
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        let resultVc = searchVc.searchResultsController as? CloudDepartmentResultController
        resultVc?.loadData(department: searchBar.text!)
    }
    
//    func updateSearchResults(for searchController: UISearchController) {
//        let resultVc = searchVc.searchResultsController as? CloudDepartmentResultController
//        resultVc?.loadData(department: searchController.searchBar.text!)
//    }
}

// MARK: - Helper
private extension CloudDepartmentController {
    func pushDoctorReserveVc(with vm: DepartmentViewModel.RowViewModel) {
//        let reserveVc = CloudDoctorReserveController(with: vm.departmentId)
//        navigationController?.pushViewController(reserveVc, animated: true)
    }
}
